from sqlalchemy.orm import Session
from . import models, schemas

def get_mobilenumber(db: Session, mobilenumber_id: int):
    return db.query(models.Mobilenumber).filter(models.Mobilenumber.id == mobilenumber_id).first()

def get_mobilenumbers(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.Mobilenumber).offset(skip).limit(limit).all()

def create_mobilenumber(db: Session, mobilenumber: schemas.MobilenumberCreate):
    db_mobilenumber = models.Mobilenumber(**mobilenumber.dict())
    db.add(db_mobilenumber)
    db.commit()
    db.refresh(db_mobilenumber)
    return db_mobilenumber

def update_mobilenumber(db: Session, mobilenumber_id: int, mobilenumber: schemas.MobilenumberUpdate):
    db_mobilenumber = db.query(models.Mobilenumber).filter(models.Mobilenumber.id == mobilenumber_id).first()
    if db_mobilenumber:
        for var, value in vars(mobilenumber).items():
            setattr(db_mobilenumber, var, value) if value is not None else None
        db.add(db_mobilenumber)
        db.commit()
        db.refresh(db_mobilenumber)
    return db_mobilenumber

def delete_mobilenumber(db: Session, mobilenumber_id: int):
    db_mobilenumber = db.query(models.Mobilenumber).filter(models.Mobilenumber.id == mobilenumber_id).first()
    if db_mobilenumber:
        db.delete(db_mobilenumber)
        db.commit()
    return db_mobilenumber